package pers.bc.utils.pub;


/**
 * *
 * Created with Description:   提供一些对象有效性校验的方法
 *
 * @fileRelativePath: JudgeAssertUtil.java
 * @Date: Created on 2018/11/4  14:24  <br>
 * @author: LiBencheng <br>
 * @version: 1.0 <br>
 */
public class JudgeAssertUtil
{
    public static String logSplitStr = "SB`";
    private static String fileDir = "thwlogs";

    /************************************************************* <br>
     *
     *说明: 斷言：最高法法官判斷<br>
     *
     * @Description   <br>
     * @Date Created on 2022/4/1  14:08  <br>
     * @param judge
     * @param message <br>
     * @return void  <br>
     * @Author LIBENCHENG
     * @version 1.0 <br>
     ************************************************************* <br>
     */
    public static void checkAssert(boolean judge, String message, Object... args)
    { if (judge) { throwException(new UnsupportedOperationException(String.format(logSplitStr + message, args))); } }

    public static void checkAssert(boolean judge, String message) { checkAssert(judge, message, ""); }

    public static void checkNull(Object object, String message)
    { if (object == null) { throwException(new NullPointerException(logSplitStr + message)); } }

    public static void throwException(String throwMsg, Object... args)
    { throwException(new RuntimeException(String.format(logSplitStr + throwMsg, args))); }

    /************************************************************* <br>
     *
     *说明: 异常处理<br>
     *
     * @Description   <br>
     * @Date Created on 2022/6/10  22:48  <br>
     * @param e <br>
     * @return void  <br>
     * @Author LIBENCHENG
     * @version 1.0 <br>
     ************************************************************* <br>
     */
    public static void throwException(Throwable e)
    {
        String[] bcs = StringUtil.valueOfNull(e.getMessage()).split(logSplitStr);
        String msg = PubEnvUtil.getSize(bcs) > 1 ? e.getMessage().replace(bcs[0], PubEnvUtil.EMPTY) : e.getMessage();
        msg = StringUtil.valueOfDefault(msg,"NPE").replace(logSplitStr, PubEnvUtil.EMPTY);

        LoggerUtil.getInstance().debug(e.getMessage() + LoggerUtil.CRLF + StringUtil.toString(e));
        LoggerUtil.getInstance(fileDir).error(e.getMessage() + LoggerUtil.CRLF + StringUtil.toString(e));
        throw new RuntimeException(msg);
    }


}
